
define(["jquery","bbcare"],function ($,$bb) {
    var servers = {};
    var URL = $bb.baseUrl("followup");

    /**
	 * 获取上一个页面传递过来的参数
	 */
    servers.getPageParams = function(){
    	return $bb.getPageParams();
    };

    //返回当前时间：
    servers.getNowFormatDate=function(){
        var date = new Date();
        var seperator1 = "-";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
        return currentdate;
    };
    //计算天数差的函数，通用 ：
    servers.dateDiff=function(sDate1,  sDate2){
        var  aDate,  oDate1,  oDate2,  iDays;
        aDate  =  sDate1.split("-");
        oDate1  =  new  Date(parseFloat(aDate[0]),parseFloat(aDate[1])-1,parseFloat(aDate[2]));    //创建时间；
        aDate  =  sDate2.split("-");
        oDate2  =  new  Date(parseFloat(aDate[0]),parseFloat(aDate[1])-1,parseFloat(aDate[2]));    //创建时间；
        iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24);    //把相差的毫秒数转换为天数
        return  iDays
    };
     /**********MABC用户建档问卷**************/
    
    
    
    servers.getBaseInfo = function (params , callback,otherParams){
        $bb.ajax({
            url: URL + "followup/service/patient/mabc/getBasicInfo",
            data: JSON.stringify({
                "data": {
                    "userId": params.userId,
                    "busiId":params.busiId
                }
            }),
            success: function (data) {
                if(data.success){
                    console.log(data)
                    callback(data,otherParams);
                }else{
                    $bb.toastShow(data.msg,1500);
                }
            }
        });
    };
    /**********MABC列表接口**************/
    servers.mabcQuestionList = function (params, callback,otherParams){
        $bb.ajax({
            url: URL + "followup/service/patient/mabc/getMabcQuestionList",
            data: JSON.stringify({
                "data": {
                    "userId": params.userId,
                    "ageState":params.ageState
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data,otherParams);
                }else{
                    $bb.toastShow(data.msg,1500);
                }
            }
        });
    };

    /*
    *  保存结果：
    * */
    servers.saveScore = function (params,callback,otherParams){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/getScore",
            data: JSON.stringify({
                "data": {
                    "examinationPaperName": params.type,
                    "postionIds": params,
                    "userId": params.userId,
                    "postionScores": params.ps
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data,otherParams);
                }else{
                    $bb.toastShow(data.msg,1500);
                }
            }
        });
    };
    /*
    * 获取指导意见：
    * */
    servers.getGuides = function (params,callback,otherParams){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/getGuide",
            data: JSON.stringify({
                "data": {
                    "examinationPaperName": params.type,
                    "postionIds": "",
                    "userId": params.userId
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data,otherParams);
                }else{
                    $bb.toastShow(data.msg,1500);
                }
            }
        });
    };
    /*
     * 保存意见：
     * */
    servers.saveGuides = function (params,callback,otherParams){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/guidemodify",
            data: JSON.stringify({
                "data": {
                    "evaluationId": params.answerId,
                    "userId": params.userId,
                    "guide":params.guide,
                    "remark":params.remark,
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data,otherParams);
                }else{
                    $bb.toastShow(data.msg,1500);
                }
            }
        });
    };
    /*
    * 按钮状态：
    * */
    servers.performanceStatus = function(totalDay,performanceStatus){
        //判断按钮的禁用状态：
        for( var i = 0;i < performanceStatus.length ;i++ ){
            if(totalDay <= (40*7+ 6*7)){
                if(performanceStatus[i].item == 'F-' || performanceStatus[i].item == 'AF' ){
                    performanceStatus[i].status = 0;
                }
            }else if(totalDay > (40*7 + 8*7) && totalDay < (40*7 + 22*7)){
                if(performanceStatus[i].item == 'Ch' || performanceStatus[i].item == 'PR' ){
                    performanceStatus[i].status = 0;
                }
            }else{
                performanceStatus[i].status = 0;
            }
        }
    }
    /*
     * 获取mabc年龄：
     * */
    servers.getDiff = function(info){
        var nowDate = servers.getNowFormatDate();
        var pregnancDay = info.pregnancDay*1;
        var diff;
        if(pregnancDay < 40*7){
            //小于40周：
            if(info.birthday){//出生： diff = 孕周 + 出生周龄
                diff = servers.dateDiff(info.birthday,nowDate) *1 + pregnancDay;
            }else{//未出生： diff = 孕周；
                diff = pregnancDay;
            }
        }else{
            //大于40周；
            if(info.birthday){//出生：diff = 出生周龄；
                diff = servers.dateDiff(info.birthday,nowDate) *1 + 40*7;
            }else{ //未出生：diff = ;  //不存在此情况：禁用：
                diff = '';
            }
        }
        return diff;
    };

    /*
    *  将结果转换为Y轴坐标：
    * */
     servers.getYval = function(res){
         var evaY;
         if(res.item == "H"){
             evaY = 2;
         }else if(res.item == "N"){
             evaY = 1;
         }else if(res.item == "PR"){
             evaY = 3;
         }else if(res.item == "Ch"){
             evaY = 4;
         }else if(res.item == "CS"){
             evaY = 5;
         }else if(res.item == "AF"){
             evaY = 6;
         }else{
             evaY = 7;
         }
         return evaY;
     }
     servers.getRes = function(res){
         var evaY;
         if(res.item == "H"){
             evaY = "不安运动阶段的正常mabc";
         }else if(res.item == "N"){
             evaY = "扭动阶段正常mabc";
         }else if(res.item == "PR"){
             evaY = '“单调性”mabc';
         }else if(res.item == "Ch"){
             evaY = '“混乱性”mabc';
         }else if(res.item == "CS"){
             evaY = '“痉挛-同步性”mabc';
         }else if(res.item == "AF"){
             evaY = '“异常性”不安运动';
         }else{
             evaY = '不安运动缺乏';
         }
         return evaY;
     }
     servers.curDeal = function(data){
         var options = data.option;
         var result = data.result;
         var history = [];
         for(var  i = 0;i<result.length;i++){
             var res = JSON.parse(result[i].result);
             res.testTime = result[i].testTime;
             res.evaRes = options[res.item];
             res.itemNum = servers.getYval(res);
             history.push({
                 name:JSON.stringify(res),
                 value: [res.days, res.itemNum],
                 itemStyle: {
                     normal: {
                         color: res.color
                     },
                     emphasis: {
                         color: res.color
                     }
                 }
             });
         }
         return history;
     }
    return servers;
});